package org.puzzle2.effect;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.media.Effect;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TrackEffectPanel extends JPanel implements Observer {
	TrackEffect2 effect;

	JTextField hueSimilarityTF;

	JTextField saturationSimilarityTF;

	JTextField brightnessSimilarityTF;

	JTextField hueTF;

	JTextField saturationTF;

	JTextField brightnessTF;

	JTextField redTF;

	JTextField greenTF;

	JTextField blueTF;

	public TrackEffectPanel() {

		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		// similarity
		JPanel tfPanel = new JPanel();
		tfPanel.setLayout(new GridLayout(0, 2));

		ActionListener listener = new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				effect.setSimilarityHSB(Float.parseFloat(hueSimilarityTF
						.getText()), Float.parseFloat(saturationSimilarityTF
						.getText()), Float.parseFloat(brightnessSimilarityTF
						.getText()));
			}
		};
		hueSimilarityTF = new JTextField(10);
		saturationSimilarityTF = new JTextField(10);
		brightnessSimilarityTF = new JTextField(10);

		hueSimilarityTF.addActionListener(listener);
		saturationSimilarityTF.addActionListener(listener);
		brightnessSimilarityTF.addActionListener(listener);

		tfPanel.add(new JLabel("Hue :"));
		tfPanel.add(hueSimilarityTF);
		tfPanel.add(new JLabel("Saturation : "));
		tfPanel.add(saturationSimilarityTF);
		tfPanel.add(new JLabel("Brightness : "));
		tfPanel.add(brightnessSimilarityTF);

		tfPanel.setBorder(BorderFactory.createTitledBorder("Similarity"));
		add(tfPanel);

		// HSB
		tfPanel = new JPanel();
		tfPanel.setLayout(new GridLayout(0, 2));

		hueTF = new JTextField(10);
		saturationTF = new JTextField(10);
		brightnessTF = new JTextField(10);

		tfPanel.add(new JLabel("Hue :"));
		tfPanel.add(hueTF);
		tfPanel.add(new JLabel("Saturation : "));
		tfPanel.add(saturationTF);
		tfPanel.add(new JLabel("Brightness : "));
		tfPanel.add(brightnessTF);
		tfPanel.setBorder(BorderFactory.createTitledBorder("Selection HSB"));
		add(tfPanel);

		// RGB
		tfPanel = new JPanel();
		tfPanel.setLayout(new GridLayout(0, 2));

		redTF = new JTextField(10);
		greenTF = new JTextField(10);
		blueTF = new JTextField(10);

		tfPanel.add(new JLabel("Red :"));
		tfPanel.add(redTF);
		tfPanel.add(new JLabel("Green : "));
		tfPanel.add(greenTF);
		tfPanel.add(new JLabel("Blue : "));
		tfPanel.add(blueTF);
		tfPanel.setBorder(BorderFactory.createTitledBorder("Selection RVB"));
		add(tfPanel);

		// Selected color
		ColorPanel colorPanel = new ColorPanel();
		tfPanel.setBorder(BorderFactory.createTitledBorder("Selected Color"));
		add(colorPanel);
	}

	public Effect getEffect() {
		return effect;
	}

	public void setEffect(Effect e) {
		effect = (TrackEffect2) e;
		effect.addObserver(this);
	}

	/**
	 * A Swing container that renders a bullseye background where the area
	 * around the bullseye is transparent.
	 */
	class ColorPanel extends JPanel {

		public ColorPanel() {
			super();
			setLayout(new BorderLayout());
			setBorder(BorderFactory.createLineBorder(Color.black));
		}

		public Dimension getPreferredSize() {
			return new Dimension(100, 100);
		}

		protected void paintComponent(Graphics g) {
			Dimension size = getSize();
			float[] hsb = effect.getSelectedColorHSB();
			Color color = null;
			try {
				color = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
			} catch (Exception e) {
				return;
			}
			g.setColor(color);
			g.fillRect(10, 20, (int) size.getWidth() - 10, (int) size
					.getHeight() - 10);
		}
	}

	public void update(Observable arg0, Object arg1) {
		if (effect.getSelectionClick() == null) {
			return;
		}
		float[] sim = effect.getSimilarityHSB();
		float[] hsb = effect.getSelectedColorHSB();
		Color color = null;
		try {
			color = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
		} catch (Exception e) {
			return;
		}
		hueSimilarityTF.setText(Float.toString(sim[0]));
		saturationSimilarityTF.setText(Float.toString(sim[1]));
		brightnessSimilarityTF.setText(Float.toString(sim[2]));

		hueTF.setText(Float.toString(hsb[0]));
		saturationTF.setText(Float.toString(hsb[1]));
		brightnessTF.setText(Float.toString(hsb[2]));

		redTF.setText("" + color.getRed());
		greenTF.setText("" + color.getGreen());
		blueTF.setText("" + color.getBlue());
		repaint();
	}
}
